package com.block.risk.service.type.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.risk.model.result.RiskJudgeResult;
import com.block.risk.service.IDefaultItemService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ZaiYangItemService implements IDefaultItemService {

    private List<String> valueListSelf = Arrays.asList("0", "(0,500)", "[500,1000)");

    @Override
    public RiskJudgeResult getControlResult(JSONObject resultDetial) {

        /**
         * 一个月内还款金额大于1000 6个月内逾期笔数小于2
         */

        RiskJudgeResult riskJudgeResult = new RiskJudgeResult();
        riskJudgeResult.setPass(false);

        JSONObject currentReportDetail = resultDetial.getJSONObject("current_report_detail");
        JSONObject behaviorReportDetail = resultDetial.getJSONObject("behavior_report_detail");
        JSONObject applyReportDetail = resultDetial.getJSONObject("apply_report_detail");

        if (ObjectUtil.isEmpty(behaviorReportDetail)){
            riskJudgeResult.setResult("行为数据结果为空；【behavior_report_detail】");
            return riskJudgeResult;
        }

        // B22170040 近1 个月履约贷款总金额
        String repayAmount = behaviorReportDetail.getString("B22170040");
        if (valueListSelf.contains(repayAmount)){
            riskJudgeResult.setResult(StrUtil.format("一个月还款款金额>1000不通过；【{}】", repayAmount));
            return riskJudgeResult;
        }

        /**
         * 近6 个月 MI+逾期贷款笔数
         */
        String numberSettle = behaviorReportDetail.getString("B22170028");
        if (Integer.valueOf(numberSettle) > 1){
            riskJudgeResult.setResult(StrUtil.format("近6 个月 MI+逾期贷款笔数小于2不通过；【{}】", numberSettle));
            return riskJudgeResult;
        }

        riskJudgeResult.setPass(true);
        riskJudgeResult.setResult("全景分控通过");
        return riskJudgeResult;
    }
}
